/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

import bl.GamePanal;
import java.awt.BorderLayout;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


/**
 *
 * @author Alexander
 */
public class SPACE_Main_Window extends JFrame implements Runnable
{
    private JPanel pnAll;
    private Thread thread;
    private Draw draw;
    private int fps = 30;
    
    private final GraphicsDevice device;
    private DisplayMode originalDisplayMode;
    private DisplayMode gameDisplayMode;
    
    public SPACE_Main_Window(GraphicsDevice defaultDevice) throws HeadlessException 
    {
        device = defaultDevice;
        init();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        thread.start();
    }
//    
//    public SPACE_Main_Window() throws HeadlessException 
//    {
//        device = null;
//        init();
//        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//        this.setVisible(true);
//        draw = new Draw((Graphics2D)pnAll.getGraphics(), pnAll);
//        thread.start();
//    }
    
    public void init()
    {
        if(device.isFullScreenSupported())
        {
            setUndecorated(true);
            setVisible(true);
            originalDisplayMode = device.getDisplayMode();
            gameDisplayMode = new DisplayMode(originalDisplayMode.getWidth(), originalDisplayMode.getHeight(), originalDisplayMode.getBitDepth(),  DisplayMode.REFRESH_RATE_UNKNOWN);
            device.setFullScreenWindow(this);
            device.setDisplayMode(gameDisplayMode);
        }
        draw = new Draw(gameDisplayMode.getWidth(), gameDisplayMode.getHeight());
        
//        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
//        this.setUndecorated(true);
//        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//        this.setSize((int)screenSize.getWidth(), (int)screenSize.getHeight());
        
        
        
        pnAll = new GamePanal(draw);
        pnAll.setDoubleBuffered(true);
        pnAll.addMouseListener(new MouseAdapter() 
        {
            @Override
            public void mousePressed(MouseEvent e) 
            {
                super.mouseClicked(e);
                Point mousePoint = MouseInfo.getPointerInfo().getLocation();
                draw.mouseClicked(mousePoint);
            }

            @Override
            public void mouseReleased(MouseEvent e) 
            {
                super.mouseReleased(e);
                draw.mouseReleased();
            }
        });
        this.setLayout(new BorderLayout());
        this.add(pnAll, BorderLayout.CENTER);
        thread = new Thread(this);
    }
    
    @Override
    public void run() 
    {
        long startTime = 0;
        long deltaTime = 0;
        double sleepTime = 0;
        while(true)
        {
            startTime = System.currentTimeMillis();
            update();
            repaint();
            Toolkit.getDefaultToolkit().sync();
            deltaTime = System.currentTimeMillis() - startTime;
            sleepTime = 1000/fps - deltaTime;
            if(sleepTime < 0)
            {
                sleepTime = 0;
            }
            try {
                Thread.sleep((long)sleepTime);
            } catch (InterruptedException ex) {
                Logger.getLogger(SPACE_Main_Window.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    public void update()
    {
        Point mousePoint = MouseInfo.getPointerInfo().getLocation();
        draw.mouseMove(mousePoint);
    }
    public static void main(String[] args) 
    {
//        SPACE_Main_Window smw = new SPACE_Main_Window();
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice defaultDevice = env.getDefaultScreenDevice();
                SPACE_Main_Window smw = new SPACE_Main_Window(defaultDevice);
            }
        });
    }
}
